﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NReader.Service;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “分组项页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234231 上提供

namespace NReader
{
    public enum SortType
    {
        SortQ = 1001, //全本
        SortX = 1002, //玄幻奇幻
        SortW = 1003, //武侠仙侠
        SortD = 1004, //都市言情
        SortL = 1005, //历史军事
        SortY = 1006, //游戏竞技
        SortK = 1007, //科幻灵异
        SortM = 1008, //漫画同人
        SortS = 3001 //查询
    }

    /// <summary>
    /// 显示分组的项集合的页。
    /// </summary>
    public sealed partial class Index : NReader.Common.LayoutAwarePage
    {
        public Index()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: 将可绑定组集合分配到 this.DefaultViewModel["Groups"]
            var list = await GlobalService.Instance().GetIndexGroupList();
            cvs2.Source = list;
        }

        private void ItemsByLetterItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.ClickedItem != null)
            {
                int articleid = ((NReader.Service.RGS.ArticleInfo)e.ClickedItem).articleid;
                this.Frame.Navigate(typeof(Book), articleid);
            }
        }

        private void BtnSortClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Windows.UI.Xaml.Controls.Button;
            if (button == null) return;
            switch (button.Content.ToString())
            {
                case "Q":
                    this.Frame.Navigate(typeof(BookList), SortType.SortQ);
                    break;
                case "X":
                    this.Frame.Navigate(typeof(BookList), SortType.SortX);
                    break;
                case "W":
                    this.Frame.Navigate(typeof(BookList), SortType.SortW);
                    break;
                case "D":
                    this.Frame.Navigate(typeof(BookList), SortType.SortD);
                    break;
                case "L":
                    this.Frame.Navigate(typeof(BookList), SortType.SortL);
                    break;
                case "Y":
                    this.Frame.Navigate(typeof(BookList), SortType.SortY);
                    break;
                case "K":
                    this.Frame.Navigate(typeof(BookList), SortType.SortK);
                    break;
                case "M":
                    this.Frame.Navigate(typeof(BookList), SortType.SortM);
                    break;
                case "C":
                    this.Frame.Navigate(typeof(Link), SortType.SortM);
                    break;
            }
        }

        private void BtnSearchClick(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(BookList), SortType.SortS);
        }

        private void BtnLoginClick(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Login));
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(BookList), SortType.SortS);
        }

    }
}
